package com.rpj.stauy.controller;

import cn.hutool.core.date.DateUtil;
import com.rpj.stauy.pojo.PromptVo;
import com.rpj.stauy.service.PromptChatAssistent;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping
public class MessageController {

    @Resource
    public PromptChatAssistent promptChatAssistent;
    @Resource
    public ChatModel chatModel;

    //http://localhost:9097/message/chat
    @GetMapping("message/chat")
    public String messageChat(){
        String chatted = promptChatAssistent.chat("static作用", 200);
        System.out.println(chatted);
        String chatted1 = promptChatAssistent.chat("秃头会遗传吗", 200);
        System.out.println(chatted1);
        String chatted2 = promptChatAssistent.chat("蛋炒饭怎么做", 200);
        System.out.println(chatted2);
        return chatted2;
    }


    //http://localhost:9097/message/chattwo
    @GetMapping("message/chattwo")
    public String messageChatTwo(){
        PromptVo promptVo = new PromptVo();
        promptVo.setPrompt("static作用");
        promptVo.setType("java");
        String chatted = promptChatAssistent.chatTwo(promptVo);
        System.out.println(chatted);
        promptVo.setPrompt("秃头会遗传吗");
        promptVo.setType("java");
        String chatted1 = promptChatAssistent.chatTwo(promptVo);
        System.out.println(chatted1);
        promptVo.setPrompt("蛋炒饭怎么做");
        promptVo.setType("java");
        String chatted2 = promptChatAssistent.chatTwo(promptVo);
        System.out.println(chatted2);
        return chatted2;
    }


    /**
     * 不懂，it完全没效果
     * it参数存在疑问，需要重新学
     * @return
     */
    //http://localhost:9097/message/chatthree
    @GetMapping("message/chatthree")
    public String messageChatThree(){
        String role = "java";
        String question = "秃头会遗传吗";
        PromptTemplate promptTemplate = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");
        Prompt prompt = promptTemplate.apply(Map.of("it", role, "question", question));
        UserMessage userMessage = prompt.toUserMessage();
        ChatResponse chatResponse = chatModel.chat(userMessage);
        System.out.println(chatResponse.aiMessage().text());
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chatResponse.aiMessage().text();
    }


}
